package com.sunmouse.toollib.utils;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.sunmouse.toollib.R;

/**
 * Created by Kvin on 2017/1/5.
 */
public class DialogUtil extends AlertDialog {

    private View mContentView;
    private int themeId;
    private int mAnimId;
    private int mWidth;
    private int mHeight;
    private int mMode;

    private DialogUtil(Context context, Builder builder) {
        this(context, builder.getThemeId() < 0 ? 0 : builder.getThemeId());
        this.mContentView = builder.getView();
        this.mAnimId = builder.getAnimId() < 0 ? FADE_IN_FADE_OUT : mAnimId;
        this.mWidth = builder.getWidth();
        this.mHeight = builder.getHeight();
        this.mMode=(builder.getMode()!=Builder.INPUT)?Builder.NORMAL:builder.getMode();
    }

    private DialogUtil(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mContentView == null) {
            throw new RuntimeException("Error content view is null");
        }
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setWindowAnimations(mAnimId);//set loading animation

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = mWidth;
        params.height = mHeight;
        getWindow().setAttributes(params);
        if (mMode==Builder.INPUT) {
//            setView(mContentView);
            return;
        }
        setContentView(mContentView);
    }


    /**
     * show
     */
    @Override
    public void show() {
        if (mMode==Builder.INPUT){
            setView(mContentView);
        }
        super.show();
    }

    /**
     * init
     */
    public static Builder init() {
        return new Builder();
    }


    /**
     * builder
     */
    public static class Builder {
        public static final int NORMAL=1;
        public static final int INPUT=2;
        private View mView;
        private int themeId;
        private int animId;
        private int width;
        private int height;
        private int mode;
        /**
         * set content view
         *
         * @param contentView
         * @return
         */
        public Builder view(View contentView) {
            this.mView = contentView;
            return this;
        }

        public Builder themeId(int themeId) {
            this.themeId = themeId;
            return this;
        }

        public Builder animId(int animId) {
            this.animId = animId;
            return this;
        }

        public Builder width(int width) {
            this.width = width;
            return this;
        }

        public Builder height(int height) {
            this.height = height;
            return this;
        }
        public Builder mode(int mode) {
            this.mode=mode;
            return this;
        }

        public View getView() {
            return mView;
        }

        public int getThemeId() {
            return themeId;
        }

        public int getAnimId() {
            return animId;
        }

        public int getHeight() {
            return height;
        }

        public int getWidth() {
            return width;
        }

        public int getMode() {
            return mode;
        }

        /**
         * build
         */
        public DialogUtil build(Context context) {
            return new DialogUtil(context, this);
        }
    }


    /**
     * anim
     */
    public static final int TOP_IN_TOP_OUT = R.style.Transition_TopInTopOut;
    public static final int FADE_IN_FADE_OUT = R.style.Transition_FadeInFadeOut;
    public static final int THEME_TRANSPARENT = R.style.DialogUtil_Transparent;
    public static final int THEME_NORMAL = R.style.DialogUtil_Normal;
}
